
import 'package:elevator/model/build_info.dart';
import 'package:elevator/model/pum_info.dart';
import 'package:elevator/model/pus_info.dart';
import 'package:elevator/model/signal_info.dart';
import 'package:flutter/material.dart';

import 'alerm_info.dart';

class LiftInfo{

   ///电梯序号 唯一标识
   int order = -1;
   ///电梯当前地址 显示楼层数
   String currentAddr = "";

   String liftName ="";
   ///LiftType=0为普通梯，LiftType=1为扶手梯
   int  liftType = 0;

   //0表示手扶梯 1表示普通梯未开  2表示正开门 3表示侧门开
   int liftOpenType = 0;

   ///电梯状态信息 0 up, 1 down,2 move,3 bell ,-1 未知
   int currentState = -1;

   //报警信息
   List<AlermInfo> alermList = [];

   //报警信息
   List<AlermInfo> spvAlermList = [];

   //报警信息
   List<AlermInfo> tpcAlermList = [];

   String day = "";
   String version = "";

   int speed = -1;
   int load = -1;

   String? dls;
   String? lds;
   String? pulse;

   //165-227：Elevator_data： 0x300-0x33E（63字节）
   Iterable<int> elevatoDataList = [];

   DateTime stateUpdatetime = DateTime.now();

   PusInfo? pusInfo;

   PumInfo? puwInfo;

   String? svpStr;

   void updateState(){
     stateUpdatetime = DateTime.now();
   }

   /// 30秒没更新状态 清除状态
   void checkClearState(DateTime now){
      if(null==stateUpdatetime || now.difference(stateUpdatetime).inSeconds > 30){
         resetState();
      }
   }

   void resetState() {

      liftOpenType = 0;
      ///电梯状态信息 0 up, 1 down,2 move,3 bell ,-1 未知
      currentState = -1;

      //报警信息
      alermList = [];
      tpcAlermList = [];
      spvAlermList = [];

      day = "";
      version = "";

      speed = -1;
      load = -1;

      elevatoDataList = [];

      pusInfo = null;

      puwInfo = null;

      svpStr = null;

   }

   getLoadName(){
      String loadMsg = "Loading:~";
      if(1==load){
         loadMsg = "Loading:0%~20%";
      }else if(3==load){
         loadMsg = "Loading:20%~40%";
      }else if(7==load){
         loadMsg = "Loading:40%~50%";
      }else if(15==load){
         loadMsg = "Loading:50%~60%";
      }else if(31==load){
         loadMsg = "Loading:60%~80%";
      }else if(63==load){
         loadMsg = "Loading:80%~100%";
      }else if(127==load){
         loadMsg = "Loading:100%~105%";
      }else if(255==load){
         loadMsg = "Loading:>105%";
      }
      return loadMsg;
   }

   getSpeedName(){
      if(speed>=0){
         return "${speed}M/min";
      }else{
         return "~M/min";
      }
   }

   @override
   String toString() {
    return 'LiftInfo{order: $order, currentAddr: $currentAddr, liftName: $liftName, currentState: $currentState, alermList: $alermList}';
   }

   getTpcSignalBgColor(SignalInfo signalInfo){
      if(elevatoDataList.isNotEmpty && elevatoDataList.length==63){
         if(signalInfo.sls3Adr.isNotEmpty){
            var addr = int.tryParse(signalInfo.sls3Adr.replaceAll("0x", ""),radix: 16);
            if(null != addr){
               int tpcIndex = addr - 0x300;
               if(tpcIndex>=0 && tpcIndex<elevatoDataList.length){
                  int tpc = elevatoDataList.elementAt(tpcIndex);
                  String tpcBuffer = BuildInfo.reversedStr(BuildInfo.intToRadixString(tpc));
                  if(tpcBuffer.length==8){
                     signalInfo.bit0bg = getColorByChar(tpcBuffer.substring(0,1));
                     signalInfo.bit1bg = getColorByChar(tpcBuffer.substring(1,2));
                     signalInfo.bit2bg = getColorByChar(tpcBuffer.substring(2,3));
                     signalInfo.bit3bg = getColorByChar(tpcBuffer.substring(3,4));
                     signalInfo.bit4bg = getColorByChar(tpcBuffer.substring(4,5));
                     signalInfo.bit5bg = getColorByChar(tpcBuffer.substring(5,6));
                     signalInfo.bit6bg = getColorByChar(tpcBuffer.substring(6,7));
                     signalInfo.bit7bg = getColorByChar(tpcBuffer.substring(7,8));
                  }

               }
            }

         }
      }
   }

   getColorByChar(String char){
      if("1".compareTo(char)==0){
         return Colors.orange;
      }else{
         return Colors.transparent;
      }
   }

}